﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Yule8xCommon.Applib
{
    public class WechatUrls
    {
        /// <summary>
        /// 获取普通access_token
        /// <para>access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时，需定时刷新，重复获取将导致上次获取的access_token失效。</para>
        /// </summary>
        /// <remarks>成功：{"access_token":"ACCESS_TOKEN","expires_in":7200}</remarks>
        /// <remarks>失败：{"errcode":40013,"errmsg":"invalid appid"}</remarks>
        public static string GetBaseTokenUrl()
        {
            return string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppInfo.AppID, AppInfo.AppSecret);
        }
        public static string GetUserInfoUrl(string src,int referral_id=0)
        {
            string callbackurl = string.Format("https://wx.34537.com/Login/LoginCallback.aspx?src={0}&referral_id={1}",src, referral_id);
            // snsapi_base
            // 成功：{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN", "openid":"OPENID", "scope":"SCOPE" }
            // 失败:{"errcode":40029,"errmsg":"invalid code"} 
            // snsapi_userinfo
            // 成功:{ "openid":" OPENID", " nickname": NICKNAME, "sex":"1", "province":"PROVINCE" "city":"CITY", "country":"COUNTRY", "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", "privilege":[ "PRIVILEGE1" "PRIVILEGE2" ], "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }
            // 失败:{"errcode":40003,"errmsg":" invalid openid "} 
            string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect", AppInfo.AppID, System.Web.HttpContext.Current.Server.UrlEncode(callbackurl));
            return url;

        }
    }
}